package com.bdy.lm.taximanager.thread;

import com.bdy.lm.taximanager.app.MyApp;
import com.bdy.lm.taximanager.model.SerialDataEvent;
import com.bdy.lm.taximanager.model.TransitionInfo;
import com.bdy.lm.taximanager.port.TSerialPort;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/8/14.
 */
public class SerialDataThread extends Thread implements TSerialPort.TSerialPortDelegate {

    private SerialDataEvent serialDataEvent = new SerialDataEvent();
    private String flag = "串口线程";
    private TSerialPort mTSerialPort;
    private GetSerialData getSerialData = null;
    //private byte[] data = {125};//大小为512字节
    //private List data = new ArrayList();

    public SerialDataThread() {
        // 打开串口接收计价器的数据
        mTSerialPort = new TSerialPort(this);
        try {
            mTSerialPort.open("ttyTCC2", 2400);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public SerialDataThread(GetSerialData getSerialData) {
        mTSerialPort = new TSerialPort(this);
        try {
            mTSerialPort.open("ttyTCC2", 2400);
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.getSerialData = getSerialData;
    }
    @Override
    public void processSerialPortData(List data) {
    //    for (int i = 0; i < length; i++) {
    //        System.out.print("Demo："+ data[i]);
            //this.data.add(data[i]);
    //    }
        //System.out.println("Demo的测试长度："+ length);

        //this.data = concat(this.data, data);


        //System.out.println("Demo的测试长度："+ this.data.length);

//        for(int i = 0; i<this.data.length; i++) {
//            System.out.print("*Demo"+ this.data[i]);
//        }
    }

    public void run() {
        while (MyApp.threadRun) {
            try {
                Thread.sleep(1000);
                //System.out.println("\nDemoSize"+ MyApp.data.size());
                if (MyApp.data.size() >= 18) {
                    if ((byte) MyApp.data.get(0) == (byte)0xf0) {//开机信息
                        MyApp.data.clear();
                    } else if ((byte) MyApp.data.get(2) == 1 && (byte) MyApp.data.get(3) == 1) {//上车信息
                        responseState();
                        MyApp.runState = "working";
                        //System.out.println("Demo上车了"+data.get(2) + "||"+data.get(3));
                        begin();
                        System.out.println("Demo上车了||" + MyApp.runState + "||" + MyApp.data.get(2) + "||"+ MyApp.data.get(3)+"已清除");
                        MyApp.data.clear();
                        //getSerialData.beginVerify();
                    } else if ((byte) MyApp.data.get(1) == 36 && MyApp.data.size() >= 27) {//下车信息
                        send();
                        MyApp.runState = "none";
                        responseData(0);
                        System.out.println("Demo下车" + MyApp.runState + "size:" + MyApp.data.size());
                        if(MyApp.data.size() == 99){
                            MyApp.data.clear();
                            System.out.println("Demo下车已清除");
                        }
                    } else {
                        MyApp.data.clear();//上下车之间不超过一分钟
                    }
                }
            } catch (InterruptedException e) {
            }
            //serialDataEvent.setMoney(SerialData.getSerialData());
            //serialDataEvent.setMoney();
            //System.out.println("串口："+data[0]);
            //EventBus.getDefault().post(serialDataEvent);
            //Log.d(flag,"开始位置发送");
        }
    }

    private void begin() {
        serialDataEvent.setNull();
        System.out.println("Demo已发送begin");
        EventBus.getDefault().post(serialDataEvent);
    }

    public void send() {

        TransitionInfo transitionInfo = TransitionInfo.newInstance(MyApp.data);
        serialDataEvent.setMoney(Double.toString(transitionInfo.getSum()));
        serialDataEvent.setPrice(Double.toString(transitionInfo.getPrice()));
        serialDataEvent.setDistance(Double.toString(transitionInfo.getDistance()));
        serialDataEvent.setTime(transitionInfo.getHour()+":"+transitionInfo.getMinute());
        serialDataEvent.setJourney(transitionInfo.getJourney());
        if ((System.currentTimeMillis() - MyApp.startEvaluate) > 2000) {
            MyApp.startEvaluate = System.currentTimeMillis();
            EventBus.getDefault().post(serialDataEvent);
            System.out.println("Demo已发送send");
        }
        if (getSerialData != null) {
            //getSerialData.finishVerify(transitionInfo.getDistance());
        }
        //Log.d("SerialDataSend",new MessagePackaging(110).getTransactionMessagePackageMap(transitionInfo.getTransInfo()).toString());
    }

    public void responseState() {
        byte[] d = new byte[6];
        d[0] = (byte) 0xaa;//起始
        d[1] = 3;//长度
        d[2] = 1;//类型
        d[3] = 1;//内容
        d[4] = (byte) (3 + 2 + 1);
        d[5] = (byte) 0xcc;//结束
        mTSerialPort.sendData(d);
    }

    private void responseData(int isSuccess) {
        byte[] d = new byte[6];
        d[0] = (byte) 0xaa;//起始
        d[1] = 3;//长度
        d[2] = 2;//类型
        d[3] = 0;//内容
        d[4] = (byte) (3 + 2 + isSuccess);
        d[5] = (byte) 0xcc;//结束
        mTSerialPort.sendData(d);
    }

    public interface GetSerialData{
        void beginVerify();
        void finishVerify(double distance);
    }

}
